﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;

namespace jmwqz
{
    public partial class duihuamgr : System.Web.UI.Page
    {
        Maticsoft.BLL.tb_duihuan caBll = new Maticsoft.BLL.tb_duihuan();
        protected int page;
        protected int pageSize;
        protected int catalogId = 0;
        protected int category_id;
        protected string keywords = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pageSize = 1;
            if (!Page.IsPostBack)
            {
                RptBind();
            }
        }
        #region 数据绑定=================================
        private void RptBind()
        {
            //string _strWhere = " is_sale=0   ";
            this.page = 1;
            string strsql = "select dh.inquiretime,ud.realname,i.name,i.img_thumb,dh.id,uds.* from tb_duihuan dh " +
"left JOIN  tb_item i on dh.itemid = i.id "  +
"left JOIN  tb_userdata ud on ud.id=dh.uid " +
"LEFT JOIN ( SELECT wid,addrdetail,tel,contractPerson,pc3. NAME as province , pc2. NAME as city , pc1. NAME as area " +
"FROM wx_shop_user_addr wsu LEFT JOIN pre_common_district pc1 ON pc1.id = wsu.area "+
"LEFT JOIN pre_common_district pc2 ON pc1.upid = pc2.id "+
"LEFT JOIN pre_common_district pc3 ON pc2.upid = pc3.id) as uds on uds.wid=ud.id order by dh.inquiretime desc";

            DataSet ds = Maticsoft.DBUtility.DbHelperMySQL.Query(strsql);

            this.rptList.DataSource = ds;
            this.rptList.DataBind();
        }
        #endregion

        //下架商品
        protected void btnXiJia_Click(object sender, EventArgs e)
        {
            //ChkAdminLevel("channel_" + this.channel_name + "_list", MXEnums.ActionEnum.Edit.ToString()); //检查权限

            Maticsoft.BLL.tb_item bll = new Maticsoft.BLL.tb_item();
            Repeater rptList = this.rptList;

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                Maticsoft.Model.tb_duihuan mitem = caBll.GetModel(id);
                if (cb.Checked)
                {
                    mitem.state = "1";
                    caBll.Update(mitem);
                }
            }
            JscriptMsg("拒绝兑换成功啦！", Utils.CombUrlTxt("duihuamgr.aspx", "category_id={0}&keywords={1}", this.category_id.ToString(), this.keywords), "Success");

        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            // ChkAdminLevel("manager_list", MXEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (caBll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            // AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), "删除商品信息" + sucCount + "条，失败" + errorCount + "条"); //记录日志

            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("duihuamgr.aspx", "category_id={0}&keywords={1}", this.category_id.ToString(), this.keywords), "Success");
        }
        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JscriptMsg(string msgtitle, string url, string msgcss)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }


        /// <summary>
        ///  设置操作
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            //int _pagesize;
            //if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            //{
            //    if (_pagesize > 0)
            //    {
            //        Maticsoft.Common.Utils.WriteCookie("user_list_page_size", _pagesize.ToString(), 14400);
            //    }
            //}
            //Response.Redirect(Maticsoft.Common.Utils.CombUrlTxt("duihuanmgr.aspx", "keywords={0}", this.keywords));
        }
    }
}